package hys.wssoc.modules.info.controller;

import hys.wssoc.common.utils.PageUtils;
import hys.wssoc.common.utils.R;
import hys.wssoc.modules.info.entity.CountrysideEntity;
import hys.wssoc.modules.info.service.CountrysideService;
import hys.wssoc.modules.info.vo.CountrysideAggVo;
import hys.wssoc.modules.info.vo.CountrysideSummaryVo;
import hys.wssoc.modules.info.vo.DataListByTimeVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author hys
 * @email 2439206957@qq.com
 * @date 2024-03-08 11:46:51
 */
@RestController
@RequestMapping("info/countryside")
public class CountrysideController {
    @Autowired
    private CountrysideService countrysideService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = countrysideService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{countrysideId}")
    public R info(@PathVariable("countrysideId") Integer countrysideId){
		CountrysideEntity countryside = countrysideService.getById(countrysideId);

        return R.ok().put("countryside", countryside);
    }

    @GetMapping("/summary/list")
    public R summaryList(@RequestParam(name = "cityName")String cityName){
        List<CountrysideSummaryVo> summaryList = countrysideService.summaryList(cityName);

        return R.ok().put("summaryList", summaryList);
    }

    /**
     * 根据评选的批次数搜索数据
     * @param rsTime 评选的批次。1-6的取值范围
     */
    @GetMapping("/getDataByTime/{rsTime}")
    public R detailListByTime(@PathVariable("rsTime") Integer rsTime){
        if (rsTime > 6 || rsTime < 1){
            return R.error("参数的输入不合法");
        }
        List<DataListByTimeVO> dataListByTime = countrysideService.detailListByTime(rsTime);
        return R.ok().put("dataListByTime", dataListByTime);
    }

    @GetMapping("/getAggDataByTime/{rsTime}")
    public R getAggDataByTime(@PathVariable("rsTime") Integer rsTime) throws IOException {
        List<CountrysideAggVo> aggregation = countrysideService.getAggDataByTime(rsTime);
        CountrysideAggVo result = CountrysideAggVo.builder().name("评选数据一览").children(aggregation).build();

        return R.ok().put("aggregation", result);
    }

    @GetMapping("/getAggDataByGeo")
    public R getAggDataByGeo() throws IOException {
        List<CountrysideAggVo> aggregation = countrysideService.getAggDataByGeo();

        return R.ok().put("aggregation", aggregation);
    }


    @RequestMapping("/save")
    @RequiresPermissions("info:countryside:save")
    public R save(@RequestBody CountrysideEntity countryside){
		countrysideService.save(countryside);

        return R.ok();
    }

    @RequestMapping("/update")
    @RequiresPermissions("info:countryside:update")
    public R update(@RequestBody CountrysideEntity countryside){
		countrysideService.updateById(countryside);

        return R.ok();
    }

    @RequestMapping("/delete")
    @RequiresPermissions("info:countryside:delete")
    public R delete(@RequestBody Integer[] countrysideIds){
		countrysideService.removeByIds(Arrays.asList(countrysideIds));

        return R.ok();
    }

}
